/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package centralehotel.dao;

import centralehotel.dao.interfaces.ReservationDAOLocal;
import centralehotel.modele.FicheHotel;
import centralehotel.modele.Reservation;
import java.util.Collection;
import java.util.List;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

/**
 *
 * @author Mathieu
 */
@Stateless
public class ReservationDAO implements ReservationDAOLocal {
    @PersistenceContext
    private EntityManager em;

    public void create(Reservation reservation) {
        em.persist(reservation);
    }

    public void edit(Reservation reservation) {
        em.merge(reservation);
    }

    public void remove(Reservation reservation) {
        em.remove(em.merge(reservation));
    }

    public Reservation find(Object id) {
        return em.find(centralehotel.modele.Reservation.class, id);
    }

    public List<Reservation> findAll() {
        return em.createQuery("select object(o) from Reservation as o").getResultList();
    }
    
    public Collection<Reservation> findReservationByHotel(FicheHotel hotel){
        Query q=em.createQuery("SELECT r FROM Reservation r WHERE r.chambre.ficheHotel = :hotel");
        q.setParameter("hotel", hotel);
        return q.getResultList();
    }
}
